package com.rd.student.ruanjian.class4.lichunquan.L_2023_7_31;

import java.util.*;

public class Demo02 {
    public static void main(String[] args) {
        Random random = new Random();
        //随机出50以内（不包括50）不重复的20个数字；
        //1.数组
        /*顺序存放-----------------------*/
        int[] arr1 = new int[50];
        for (int i = 0; i < 50; i++) {
            arr1[i] = i;
        }
        //打乱
        for (int i = 0; i < 20; i++) {
            int index = random.nextInt(50);
            int b = arr1[i];
            arr1[i] = arr1[index];
            arr1[index] = b;
        }
        //检验
        for (int i = 0; i < 20; i++) {
            System.out.println(arr1[i]);
        }
        System.out.println("1.-----------------------------");
        //2.集合-------------------------
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            list.add(i);
        }
        Collections.shuffle(list);
        //检验
        for (int i = 0; i < 20; i++) {
            System.out.println(list.get(i));
        }
        System.out.println("2.-----------------------------");
        //3.集合contains存放-------------------------
        ArrayList<Integer> list1 = new ArrayList<>();
        while (list1.size() < 20) {
            int number = random.nextInt(50);
            if (!list1.contains(number)) list1.add(number);
        }
        //检验
        for (int i = 0; i < 20; i++) {
            System.out.println(list.get(i));
        }
        System.out.println("3.-----------------------------");
        //4.set-------------------------
        HashSet<Integer> set = new HashSet<>();
        while (set.size() < 20) {
            int number = random.nextInt(50);
            set.add(number);
        }
        //检验
        for (Integer i : set) {
            System.out.println(i);
        }
        System.out.println("4.-----------------------------");
        //5.数组对比存放-------------------------
        int[] arr2 = new int[20];
        for (int index = 0; index < 20; index++) {
            int number = random.nextInt(50);
            for (int i = 0; i < index; i++) {
                while (number == arr2[i]) {
                    number = random.nextInt(50);
                    i = 0;
                }
            }
            arr2[index] = number;
        }

        //检验
        for (int j : arr2) {
            System.out.println(j);
        }
        System.out.println("5.-----------------------------");
        //6.给定数组内容判断当前元素是否有值
        int[] arr3 = new int[50];

        for (int i = 0; i < 50; i++) {
            arr3[i] = -1;
        }
        for (int i = 0; i < 20; ) {
            int index = random.nextInt(50);
            if (arr3[index] == -1) {
                arr3[index] = index;
                i++;
            }
        }
        for (int i = 0; i < 50; i++) {
            if (arr3[i] != -1) {
                System.out.println(arr3[i]);
            }
        }
        System.out.println("6.-----------------------------");
        //7.给定集合元素，取完值移除值
        LinkedList<Integer> list2 = new LinkedList<>();
        int size = list.size();
        for (int i = 0; i < 50; i++) {
            list2.add(50);
        }
        while (size > 20) {

            int index = random.nextInt(size);
            list2.remove(index);
            size--;
        }
        //检验
        for (int i = 0; i < 20; i++) {
            System.out.println(list.get(i));
        }
        System.out.println("7.-----------------------------");
    }
}
